﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Threading;
using ScrumTable.BL.DM.DataManagement;
using Microsoft.Practices.Unity;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Extension;
#endregion

namespace ScrumTable.UI.ViewModel.Tools
{
    /// <summary>
    /// View Model for a simple menu
    /// </summary>
    public class ViewModelSearchScreen : ViewModelBase
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private string _filter;
        private readonly DispatcherTimer _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.5) };
        private readonly ObservableCollection<IScrumItemViewModel<DomainBase>> _foundItems = new ObservableCollection<IScrumItemViewModel<DomainBase>>();
        private bool _checkDescription;
        private bool _checkId;
        private bool _checkAssigned;
        private bool _checkTitle;
        private bool _searchTask;
        private bool _searchStory;


        #endregion


        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        public ObservableCollection<IScrumItemViewModel<DomainBase>> FoundItems
        {
            get
            {
                if (string.IsNullOrEmpty(FilterText))
                {
                    _foundItems.Clear();
                    return _foundItems;
                }
               

                var stories =
                    (from x in ScrumData.CurrentProject.Iterations from y in x.Stories where Filter(y) select new UserStoryViewModel(y)).Union(
                        from x in ScrumData.CurrentProject.Stories where Filter(x) select new UserStoryViewModel(x));

                var allStories = (from x in ScrumData.CurrentProject.Iterations from y in x.Stories select new UserStoryViewModel(y)).Union(
                        from x in ScrumData.CurrentProject.Stories select new UserStoryViewModel(x));

                var tasks = from x in allStories from y in x.Tasks where Filter(y) select new TaskViewModel(y);


                var bugs =
                   (from x in ScrumData.CurrentProject.Iterations from y in x.Bugs where Filter(y) select new BugViewModel(y)).Union(
                       from x in ScrumData.CurrentProject.Bugs where Filter(x) select new BugViewModel(x)).Union(from x in allStories from y in x.Bugs where Filter(y) select new BugViewModel(y));

                IEnumerable<IScrumItemViewModel<DomainBase>> list = new List<IScrumItemViewModel<DomainBase>>();
                if (SearchStory) { list = list.Union(stories.Cast<IScrumItemViewModel<DomainBase>>()); }
                if (SearchTask) { list = list.Union(tasks.Cast<IScrumItemViewModel<DomainBase>>()); }
                if (SearchBug) { list = list.Union(bugs.Cast<IScrumItemViewModel<DomainBase>>()); }
                _foundItems.UnionToLeft(list.ToList());
                return _foundItems;
            }
        }


        /// <summary>
        /// The current filter, placed on all items. Least string length is 3
        /// </summary>
        public string FilterText
        {
            get { return _filter; }

            set
            {
                if (value == _filter)
                {
                    return;
                }
                SearchIsRunning = false;
                _filter = value;
                SearchIsRunning = true;
                
            }
        }

        public bool SearchIsRunning
        {
            get
            {
                return _timer.IsEnabled;
            }
            private set
            {
                if( value )
                {
                    _timer.Start();
                }
                else
                {
                    _timer.Stop();
                }
                SendPropertyChanged("SearchIsRunning");
            }

        }

        public bool SearchStory
        {
            get { return _searchStory; }
            set { _searchStory = value; FilterSettingChanged(); }
        }


        public bool SearchTask
        {
            get { return _searchTask; }
            set { _searchTask = value; FilterSettingChanged(); }
        }

        private bool _searchBug;
        public bool SearchBug
        {
            get { return _searchBug; }
            set { _searchBug = value; FilterSettingChanged(); }
        }

 
        public bool CheckTitle
        {
            get { return _checkTitle; }
            set { _checkTitle = value; FilterSettingChanged(); }
        }

        
        public bool CheckDescription
        {
            get { return _checkDescription; }
            set { _checkDescription = value; FilterSettingChanged();}
        }

        public bool CheckId
        {
            get { return _checkId; }
            set { _checkId = value; FilterSettingChanged(); }
        }

        
        public bool CheckAssigned
        {
            get { return _checkAssigned; }
            set { _checkAssigned = value; FilterSettingChanged(); }
        }

        /// <summary>
        /// Gets or sets the ScrumViewModelData
        /// </summary>
        /// <value>ScrumViewModelData</value>
        [Dependency]
        public ScrumViewModelData ScrumData { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
 
        public ViewModelSearchScreen()
        {
            CheckTitle = true;
            CheckAssigned = true;
            CheckDescription = true;
            CheckId = true;

            SearchStory = true;
            SearchTask = true;
            SearchBug = true;
            
            _timer.Tick += (sender, args) =>
            {
                SearchIsRunning = false;
                SendPropertyChanged("FoundItems");
            };
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        public void FilterSettingChanged()
        {
            if(!string.IsNullOrEmpty(FilterText))
            {
                SearchIsRunning = false;
                SendPropertyChanged("FoundItems");
            }
        }
        private bool Filter(IScrumItem item)
        {
            string text = FilterText.ToUpper();

            if (CheckTitle && item.Name.ToUpper().Contains(text) 
                || CheckDescription && item.Description.ToUpper().Contains(text) 
                || CheckId && item.BackEndId == text
                || CheckAssigned && item.AssignedMember != null && item.AssignedMember.Name.ToUpper().Contains(text))
            {
                return true;
            }
            return false;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        
      

        #endregion

    }
}